package my.story;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.story.db.DBUtils;

/**
 * Servlet implementation class Show
 */
@WebServlet("/Show")
public class Show extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Show() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql = "select * from piccomment";
		Connection conn = DBUtils.getConnection();
		Statement st = null;
		ResultSet rs = null;
		List<PicBean> pics = new ArrayList<PicBean>();
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next()){
				PicBean pic = new PicBean();
				String picName = rs.getString(2);
				String comment = rs.getString(3);
				pic.setComment(comment);
				pic.setPicName(picName);
				pics.add(pic);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.release(rs);
			DBUtils.release(st);
			DBUtils.release(conn);
		}
		request.getSession().setAttribute("pics", pics);
		response.sendRedirect("lovestory.jsp");
	}

}
